/*=================================================================*/
/*               IS-2000  TOD                                      */
/*=================================================================*/
#ifndef H_RCGD_TOD_H
#define H_RCGD_TOD_H

#define	CGM_P()		(OSC.kind==Rubidium_OSC)
#define	CGM_S()		(OSC.kind==OCXO_OSC)


#define	Module_CGM_P	0
#define	Module_CGM_S	1


#define Module_A()	(SYSTEM.ID==ModuleA)



typedef struct TOD_S{
    unsigned char	turn;
    unsigned char	printon;
    
    unsigned char	status;
    unsigned char   error;
    unsigned char	opmode;
    unsigned char   inform;

    unsigned char	BUFFER[20];
    short			SKIP_TIMER;
    short			SKIP_TOD_TIMER;

	time_	time;
	int		match_counter;
} TOD_;


typedef struct {

	unsigned char	year10;
	unsigned char	year1;

	unsigned char	day100;
	unsigned char	day10;
	unsigned char	day1;

	unsigned char	hour10;
	unsigned char	hour1;

	unsigned char	minute10;
	unsigned char	minute1;

	unsigned char	second10;
	unsigned char	second1;

	unsigned char	leapsecond10;
	unsigned char	leapsecond1;

	unsigned char	status;
	unsigned char	error;

	unsigned char	opmode;
	unsigned char	inform;
	unsigned char	tail;

} R_TOD_;


typedef struct {
	int fpga_addr;
	int length;
	R_TOD_ rtod;
} F_TOD_;


#define	BIT_STATUS_WARMUP			0x01
#define	BIT_STATUS_HOLDOVER			0x02
#define	BIT_STATUS_ANT_FAULT		0x04
#define	BIT_STATUS_1PPS_ALM			0x08
#define	BIT_STATUS_DAC_ALM			0x10
#define	BIT_STATUS_TEMP_ALM			0x20
#define	BIT_STATUS_OFFSET_ALM		0x40
#define	BIT_STATUS_FAULT			0x80

#define	BIT_ERROR_NO_GPS_TIME		0x01
#define	BIT_ERROR_OSC_FAIL			0x02
#define	BIT_ERROR_1PPS_FAIL			0x04
#define	BIT_ERROR_10M_FAIL			0x08
#define	BIT_ERROR_19M_FAIL			0x10
#define	BIT_ERROR_NO_MODULE			0x20
#define	BIT_ERROR_PP2S_FAIL			0x40
#define	BIT_ERROR_UNKNOWN_FAULT		0x80



//------------------------------------------------------------------------
#define BIT_STATUS_HOLD_OVER			0x01
#define BIT_STATUS_GPS_FAIL			0x02
#define BIT_STATUS_OSC_FAIL			0x08



#define BIT_ERROR_TFOM				0x40
#define BIT_ERROR_A_OPEN			0x20
#define BIT_ERROR_B_OPEN			0x10
#define BIT_ERROR_A_FUNC_FAIL			0x08
#define BIT_ERROR_B_FUNC_FAIL			0x04
#define BIT_ERROR_FAN_OPEN			0x02
#define BIT_ERROR_FAN_FAIL			0x01



#define BIT_OPMODE_WARMUP_ACT			0x00
#define BIT_OPMODE_COAST_ACT			0x40
#define BIT_OPMODE_RECOVER_ACT			0x80
#define BIT_OPMODE_LOCKED_ACT			0xC0

#define BIT_OPMODE_WARMUP_A			0x00
#define BIT_OPMODE_COAST_A			0x10
#define BIT_OPMODE_RECOVER_A			0x20
#define BIT_OPMODE_LOCKED_A			0x30

#define BIT_OPMODE_WARMUP_B			0x00
#define BIT_OPMODE_COAST_B			0x04
#define BIT_OPMODE_RECOVER_B			0x08
#define BIT_OPMODE_LOCKED_B			0x0C

#define BIT_OPMODE_RESYNC			0x02
#define BIT_OPMODE_INIT				0x01



#define BIT_INFO_OVER_TFOM_A			0x80
#define BIT_INFO_OVER_TFOM_B			0x40
#define BIT_INFO_ACT_MODULE			0x20

#define BIT_INFO_ANT_GOOD_A			0x00
#define BIT_INFO_ANT_OPEN_A			0x04
#define BIT_INFO_ANT_SHORT_A			0x08
#define BIT_INFO_ANT_DISABLED_A			0x0C

#define BIT_INFO_ANT_GOOD_B			0x00
#define BIT_INFO_ANT_OPEN_B			0x01
#define BIT_INFO_ANT_SHORT_B			0x02
#define BIT_INFO_ANT_DISABLED_B			0x03
//------------------------------------------------------------------------



#define BIT_COASTING		0x01

#define BIT_A_RTC_FAIL		0x02
#define BIT_B_RTC_FAIL		0x04
#define BIT_A_OSC_FAIL		0x08
#define BIT_B_OSC_FAIL		0x10
#define BIT_A_POWER_FAIL	0x20
#define BIT_B_POWER_FAIL	0x40

#define BIT_OVER_TFOM		0x80


#define BIT_A_OPEN			0x01
#define BIT_B_OPEN			0x02
#define BIT_A_OSC_OPEN		0x04
#define BIT_B_OSC_OPEN		0x08
#define BIT_A_POWER_OPEN	0x10
#define BIT_B_POWER_OPEN	0x20
#define BIT_A_ANT_OPEN		0x40
#define BIT_B_ANT_OPEN		0x80




#define BIT_A_FREE_RUN		0x00
#define BIT_A_GPS_RUN		0x40
#define BIT_A_E1_RUN		0x80
#define BIT_A_LMU_RUN		0xC0
#define BIT_A_REFERENCE		0xC0


#define BIT_B_FREE_RUN		0x00
#define BIT_B_GPS_RUN		0x01
#define BIT_B_E1_RUN		0x02
#define BIT_B_LMU_RUN		0x03
#define BIT_B_REFERENCE		0x03



#define BIT_A_LOCK			0x30
#define BIT_A_WARMUP		0x00
#define BIT_A_COASTING		0x10
#define BIT_A_RECOVERING	0x20
#define BIT_A_OPMODE		0x30

#define BIT_B_LOCK			0x0C
#define BIT_B_WARMUP		0x00
#define BIT_B_COASTING		0x04
#define BIT_B_RECOVERING	0x08
#define BIT_B_OPMODE		0x0C
#define BIT_M_OPMODE		0xC0


#define BIT_A_MODEL_CGM_P	0x80
#define BIT_B_MODEL_CGM_P	0x40
#define BIT_A_ACTIVE		0x00
#define BIT_B_ACTIVE		0x20


#define BIT_E1_REFERENCE	0x10

#define BIT_A_ANT_NORMAL	0x00
#define BIT_A_REF_OPEN		0x04
#define BIT_A_ANT_COASTING	0x08

#define BIT_A_ANT_DISABLE	0x0C

#define BIT_B_ANT_NORMAL	0x00
#define BIT_B_REF_OPEN		0x01
#define BIT_B_ANT_COASTING	0x02

#define BIT_B_ANT_DISABLE	0x03

#define BIT_A_ANT_MODE		0x0C
#define BIT_B_ANT_MODE		0x03




#define	BIT_HOLDOVER_STATUS 	0x01

#define	BIT_EB1_STATUS 			0x02
#define	BIT_EB2_STATUS 			0x04
#define	BIT_OCXO1_STATUS 		0x08
#define	BIT_OCXO2_STATUS 		0x10
#define	BIT_POWER1_STATUS 		0x20
#define	BIT_POWER2_STATUS 		0x40



#define	BIT_EXIST_EB1 			0x01
#define	BIT_EXIST_EB2           0x02
#define	BIT_EXIST_OCXO1         0x04
#define	BIT_EXIST_OCXO2         0x08
#define	BIT_EXIST_POWER1        0x10
#define	BIT_EXIST_POWER2        0x20
#define	BIT_TFOM_ERROR          0x40


//========TOD_OUT UART CONTROLER========//
#define TOD_MUX_ADDR	18

#define TOD_MUX_0		wbD2
#define TOD_MUX_1		wbD3

#define TOD_MUX_MAIN_1PPS()	do{SetBitLow(TOD_MUX_ADDR,TOD_MUX_1);	SetBitLow(TOD_MUX_ADDR,TOD_MUX_0);}while(0);
#define TOD_MUX_ADD_17()		do{SetBitLow(TOD_MUX_ADDR,TOD_MUX_1);	SetBitHigh(TOD_MUX_ADDR,TOD_MUX_0);}while(0);
#define TOD_MUX_SASC_BRG()		do{SetBitHigh(TOD_MUX_ADDR,TOD_MUX_1);	SetBitLow(TOD_MUX_ADDR,TOD_MUX_0);}while(0);
#define TOD_MUX_TOD_OUT()		do{SetBitHigh(TOD_MUX_ADDR,TOD_MUX_1);	SetBitHigh(TOD_MUX_ADDR,TOD_MUX_0);}while(0);


extern char month[12];

extern R_TOD_	R_TOD;
extern void GetRTCTime();
extern time_ setLocalTime(char, char);

//------------------------------------------------------------------------
extern time_ setLocalTimeNew(time_ SrcTime, char localh, char localm);

extern char TEMP_TOD, TEMP_TOD_MADE;

extern void DisplayTime();

extern void AutoSendODTCommand();
extern void ODTCommand(int cmd);
extern void set_tod_port_high(void);

extern unsigned char MonitorCmd;
//------------------------------------------------------------------------
extern int sync_forecast_tod;

extern F_TOD_ 	F_TOD;

extern int tod_been_out;
void increase_second_amount(time_	*time, signed char amount);
void increase_second(time_	*time);
void IncrementTOD();
void Init_TOD();
char StartTOD();
void TOD_OUT_FPGA(void);


#endif
